package com.yc.jpyy.mobile.controller;

import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.net.URI;
import java.net.URISyntaxException;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.websocket.ContainerProvider;
import javax.websocket.DeploymentException;
import javax.websocket.Session;
import javax.websocket.WebSocketContainer;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.google.gson.Gson;
import com.yc.jpyy.base.bean.Client;
import com.yc.jpyy.base.bean.RestResponse;
import com.yc.jpyy.base.config.Config;
import com.yc.jpyy.base.controller.BaseController;


@Controller
@RequestMapping(value = "/collector")
public class CollectorContoller extends BaseController{
	
	private static Logger logger = LoggerFactory.getLogger(CollectorContoller.class);
	
	public  static String uri = Config.getCompareQRCodeUrl();
	WebSocketContainer container = ContainerProvider.getWebSocketContainer();
	Client client = new Client();
    Session session = null;
	/**
	 * 向collector传参
	 * 
	 * @param request
	 * @return
	 * @throws UnsupportedEncodingException
	 */
	@SuppressWarnings("finally")
	@RequestMapping(value = "/sendToCollector", method = RequestMethod.POST, produces = "application/json; charset=utf-8")
	public @ResponseBody String sendToCollector(HttpServletRequest request)
			throws UnsupportedEncodingException {

		RestResponse response = new RestResponse();
		request.setCharacterEncoding("UTF-8");
		String json="";
		try {
			String time = request.getParameter("time");
			String type = request.getParameter("type"); 
			String vehicleId = request.getParameter("vehicleId");
			Map<String, Object> map = new HashMap<String, Object>();
			map.put("USERID", 1);
			map.put("COMMAND_ID", 3);
			map.put("commandId", "904512");
			map.put("commandType", "904");
			map.put("commandCode", "8900");
			Date date = new Date();
			String timestamp = String.valueOf(date.getTime()); 
			map.put("requestID", timestamp);
			map.put("commandBody", "");
			if(time != null && !time.isEmpty()
					&& type != null && !type.isEmpty()
					&& vehicleId != null && !vehicleId.isEmpty()){
				map.put("VEHICLEID", vehicleId);
				map.put("commandArgs", "13,8D09," + time + "," + type);
				json = new Gson().toJson(map);
				if(session != null && session.isOpen()){
					logger.info("=================开始发送消息=======================");
					session.getBasicRemote().sendText(json); // 发送文本消息
					logger.info("=================发送消息=======================");
				}else{
					logger.info("=================链接重新建立=======================");
					session = container.connectToServer(client, new URI(uri));
					session.getBasicRemote().sendText(json); // 发送文本消息
					logger.info("=================再次发送消息=======================");
				}
			}
		} catch (DeploymentException e) {
			logger.info("DeploymentException异常信息:" + e.getMessage());
			session = container.connectToServer(client, new URI(uri));
			session.getBasicRemote().sendText(json);
		} catch (IOException e) {
			logger.info("IOException异常信息:" + e.getMessage());
			session = container.connectToServer(client, new URI(uri));
			session.getBasicRemote().sendText(json);
		} catch (URISyntaxException e) {
			logger.info("URISyntaxException异常信息:" + e.getMessage());
			session = container.connectToServer(client, new URI(uri));
			session.getBasicRemote().sendText(json);
		}finally {
			return response.toJSON();
		}
	}		
}